import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

// 一级路由
const detail = () => import('../page/detail/detail.vue')
const goods = () => import('../page/goods/goods.vue')
const index = () => import('../page/index/index.vue')
const login = () => import('../page/login/login.vue')
const order = () => import('../page/order/order.vue')
const path = () => import('../page/path/path.vue')
const pay = () => import('../page/pay/pay.vue')
const search = () => import('../page/search/search.vue')

// 二级路由
const home = () => import('../page/index/components/home.vue')
const sort = () => import('../page/index/components/sort.vue')
const shop = () => import('../page/index/components/shop.vue')
const mine = () => import('../page/index/components/mine.vue')
const addpath = () => import('../page/path/addpath.vue')

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: login
    },
    {
      path: '/register',
      component: () => import('../page/login/register.vue')
    },
    {
      path: '/goods',
      component: goods
    },
    {
      path: '/detail',
      component: detail
    },
    {
      path: '/index',
      component: index,
      children: [
        {
          path: 'home',
          component: home
        },
        {
          path: 'sort',
          component: sort
        },
        {
          path: 'shop',
          component: shop
        },
        {
          path: 'mine',
          component: mine
        },
        {
          path: '',
          redirect: 'home'
        }
      ]
    },
    {
      path: '/order',
      component: order
    },
    {
      path: '/path',
      component: path,
      children: [
        {
          path: 'add',
          component:addpath
        }
      ]
    },
    {
      path: '/pay',
      component: pay
    },
    {
      path: '/search',
      component: search
    },
    {
      path:'*',
      redirect:'/'
    }
  ]
})


router.beforeEach((to, from, next) => {
  console.log(to,from);
  if(to.path=='/'){
    next()
    return
  }
  if(store.state.user.token){
    next()
    return
  }else{
    next('/')
  }
  
  // console.log(next);

})

export default router
